using System.Text.RegularExpressions;
using KenEgozi.Com.Domain.Interfaces.Services;

namespace KenEgozi.Com.Domain.Services
{
	public class UrlFriendlyStringTransformService : IUrlFriendlyStringTransformService
	{
		private static readonly Regex nonUrlCharactersFinder = new Regex("[^a-zA-Z0-9().#+ ]", RegexOptions.Compiled);

		public string Execute(string name)
		{
			string urlFriendlyName = 
				nonUrlCharactersFinder.Replace(name, "")
				.Replace(".", "-dot-")						// special chars to words
				.Replace("#", "-sharp-")
				.Replace("+", "-plus-")
				.TrimEnd('-', ' ')							// trim spaces and hyphens
				.TrimStart('-', ' ')
				.Replace("  ", " ").Replace(' ', '-')		// remaining spaces to hyphens
				.ToLower();
			return urlFriendlyName;
		}
	}
}
